<template>
	<xn-form-container
		:title="formData.id ? '编辑饲料使用记录' : '增加饲料使用记录'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="鸡舍" name="henhouseId">
				<henhouseSelect ref="henhouseRef" @update:henhouseId="handleHenhouseIdUpdate"/>
			</a-form-item>
			<a-form-item label="栏舍" name="barnId">
				<barnSelectByHenhouse ref="barnSelectRef" @update:barnId="handleBarnIdUpdate"/>
			</a-form-item>
				<a-form-item label="投放栏区" name="columnId">
					<columnSelectByBarn ref="columnSelectRef" @update:columnId="handleColumnIdUpdate"/>
				</a-form-item>
			<a-form-item label="饲料种类：" name="typeId">
<!--				<a-input v-model:value="formData.feedId" placeholder="请输入饲料种类" allow-clear />-->
				<feedTypeSelect ref="feedTypeSelectRef" :disabled="category === undefined"
								:category="category" @update:typeId="handleTypeIdUpdate"/>
			</a-form-item>
			<a-form-item label="投放数量/kg：" name="inputQuantity">
				<a-input v-model:value="formData.inputQuantity" placeholder="请输入投放数量/kg" allow-clear />
			</a-form-item>
			<a-form-item label="投放日期：" name="inputDate">
				<a-date-picker v-model:value="formData.inputDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择投放日期" style="width: 100%" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizFeedUsageRecordsForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizFeedUsageRecordsApi from '@/api/feed_records/bizFeedUsageRecordsApi'
	import bizColumnApi from "@/api/column/bizColumnApi";
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const henhouseRef = ref()
	const barnSelectRef = ref()
	const columnSelectRef = ref()
	const feedTypeSelectRef = ref()

	const category = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
		category.value = undefined;
	}
	// 默认要校验的
	const formRules = {
	}

	// 处理鸡舍id变化
	const handleHenhouseIdUpdate = (id) => {
		// 如果鸡舍id不为空则开启栏舍选择框并将id加入到formData中
		if (id !== undefined) {
			barnSelectRef.value.selectByHenhouseId(id);
			barnSelectRef.value.barnDisable = false;
		}
		else {
			formData.value.columnId = undefined;
			barnSelectRef.value.barnDisable = true;
			barnSelectRef.value.barnId = undefined;
			columnSelectRef.value.columnDisable = true;
			columnSelectRef.value.columnId = undefined;
			category.value = undefined;
		}
	}
	// 处理栏舍id变化，逻辑与处理鸡舍id类似
	const handleBarnIdUpdate = (id) => {
		if (id !== undefined) {
			formData.value.barnId = id;
			columnSelectRef.value.selectByBarnId(id);
			columnSelectRef.value.columnDisable = false;
		} else {
			formData.value.barnId = undefined;
			formData.value.columnId = undefined;
			columnSelectRef.value.columnDisable = true;
			columnSelectRef.value.columnId = undefined;
			category.value = undefined;
		}
	}

	const handleColumnIdUpdate = (id) => {
		if (id !== undefined) {
			formData.value.columnId = id;
			// 查询栏区类型，传给饲料种类选择器
			const data = {
				columnId: id
			}
			bizColumnApi.getCategoryById(data)
				.then(resp =>{
					category.value = resp;
				})
				.catch(resp =>{
					console.log(resp);
				})
		} else {
			formData.value.columnId = undefined;
			columnSelectRef.value.columnId = undefined;
		}
	}

	const handleTypeIdUpdate = (id) => {
		formData.value.typeId = id;
		console.log(formData.value.typeId);
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			bizFeedUsageRecordsApi
				.bizFeedUsageRecordsSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
			category.value = undefined;
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
